import sys
import requests as req
from pyquery import PyQuery as py
import re
import hashlib
sys.path.append()
from kkoffer.user.models import article
pq = py(url)
if url.find("weixin") > 0:
    # import ipdb; ipdb.set_trace()
    context["content"] = pq("#js_content").html()
    content = pq("#js_content").html()
    imgList = re.findall(r'data-src="(\S*)"',pq.html())
    content = content.replace('data-src',"src")
else:
    imgList = re.findall(r'src="(\S*)"',pq.html())
# req = req.session()
urlPath = '/static/upload/'
for x in imgList:
    md = hashlib.md5()
    y = x.encode('utf-8')
    md.update(y)
    key = md.hexdigest()
    path = settings.UPLOAD_DIR + key
    r = req.get(x,stream=True)
    with open(path,'wb') as w:
        for chunk in r:
            w.write(chunk)
    content = content.replace(x,urlPath + key)
title = pq("h1").text() or pq("h2").text() or pq("h3").text() or ""
# import ipdb; ipdb.set_trace()
content = re.sub("<img.{1,140}b55d7db3c2895d529a6e6453546d038e.*?>","",content)
obj = article(title=title,content=content,type=type,uid_id=request.session['id'])
obj.save()